package com.power.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.power.domain.SysRole;
import com.power.model.Result;
import com.power.service.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description 系统角色接口管理
 * @Author dingchunlin
 * @Date 2024/07/12 18:35
 * @Version 1.0.0
 */
@Api(tags = "系统角色接口管理")
@RequestMapping("/sys/role")
@RestController
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;

    /**
     * 查询系统所有角色
     * @return 角色列表
     */
    @ApiOperation(value = "查询系统所有角色")
    @GetMapping("/list")
    @PreAuthorize("hasAuthority('sys:role:list')")
    public Result<List<SysRole>> loadSysRoleList() {
        List<SysRole> sysRoleList = sysRoleService.queryRoleList();
        return Result.success(sysRoleList);
    }

    /**
     * 多条件查询分页查询系统角色
     * @param current 当前页
     * @param size 每页显示数量
     * @param roleName 角色名称
     * @return 角色分页列表
     */
    @ApiOperation(value = "多条件查询分页查询系统角色")
    @GetMapping("/page")
    @PreAuthorize("hasAuthority('sys:role:page')")
    public Result<Page<SysRole>> loadSysRolePage(@RequestParam Long current,
                                                 @RequestParam Long size,
                                                 @RequestParam(required = false) String roleName) {
        Page<SysRole> sysRolePage = new Page<>(current, size);
        LambdaQueryWrapper<SysRole> lambdaQueryWrapper = new LambdaQueryWrapper<SysRole>();
        lambdaQueryWrapper.like(StringUtils.hasText(roleName), SysRole::getRoleName, roleName);
        lambdaQueryWrapper.orderByDesc(SysRole::getCreateTime);
        Page<SysRole> page = sysRoleService.page(sysRolePage, lambdaQueryWrapper);
        return Result.success(page);
    }

    /**
     * 新增系统角色
     * @param sysRole 系统角色实体类
     * @return 成功或失败
     */
    @ApiOperation(value = "新增系统角色")
    @PreAuthorize("hasAuthority('sys:role:save')")
    @PostMapping
    public Result<String> saveSysRole(@RequestBody SysRole sysRole) {
        Boolean save =sysRoleService.saveSysRole(sysRole);
        return Result.handle(save);
    }

    /**
     * 根据id查询角色信息
     * @param id 角色id
     * @return   角色信息
     */
    @ApiOperation(value = "根据id查询系统角色")
    @GetMapping("/info/{id}")
    @PreAuthorize("hasAuthority('sys:role:info')")
    public Result<SysRole> loadSysRoleInfo(@PathVariable Long id) {
        SysRole sysRole = sysRoleService.querySysRoleInfoByRoleId(id);
        return Result.success(sysRole);
    }

    /**
     * 修改系统角色
     * @param sysRole 系统角色实体类
     * @return 成功或失败
     * @throws Exception
     */
    @ApiOperation(value = "修改系统角色")
    @PreAuthorize("hasAuthority('sys:role:update')")
    @PutMapping
    public Result<String> modifySysRole(@RequestBody SysRole sysRole) throws Exception {
        Boolean update = sysRoleService.modifySysRole(sysRole);
        return Result.handle(update);
    }

    /**
     * 批量/单个删除系统角色
     * @param roleIdList 角色id集合
     * @return 成功或失败
     */
    @ApiOperation(value = "批量/单个删除系统角色")
    @PreAuthorize("hasAuthority('sys:role:delete')")
    @DeleteMapping
    public Result<String> removeSysRole(@RequestBody List<Long> roleIdList) {
        Boolean remove = sysRoleService.removeSysRoleByIds(roleIdList);
        return Result.handle(remove);
    }

}
